package com.bnz.springboot.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.bnz.springboot.properties.JdbcProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@Configuration                                              //声明一个类作为配置类，相当于application.xml文件
//@PropertySource("classpath:properties/db.properties")       //指定属性文件的路径是：classpath:properties/db.properties
public class MyConfig {

    @Value("${db.driverClassName}")
    private String driverClassName;
    @Value("${db.url}")
    private String url;
    @Value("${db.username}")
    private String username;
    @Value("${db.password}")
    private String password;

    //为数据源赋值：方式一
//    @Bean                       //声明在方法上，将方法的返回值加入Bean容器，代替<bean>标签,相当于<bean id = "dataSource">
//    public DruidDataSource dataSource(){
//        DruidDataSource dataSource = new DruidDataSource();
//        dataSource.setDriverClassName(driverClassName);
//        dataSource.setUrl(url);
//        dataSource.setUsername(username);
//        dataSource.setPassword(password);
//        return dataSource;
//    }


    //为数据源赋值：方式二
//    @Bean
//    public DruidDataSource dataSource2(JdbcProperties jdbc){
//        DruidDataSource dataSource = new DruidDataSource();
//        dataSource.setDriverClassName(jdbc.getDriverClassName());
//        dataSource.setUrl(jdbc.getUrl());
//        dataSource.setUsername(jdbc.getUsername());
//        dataSource.setPassword(jdbc.getPassword());
//        return dataSource;
//    }


    //为数据源赋值：方式三
    @Bean
    @ConfigurationProperties(prefix = "db")
    public DruidDataSource druidDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }
}
